package com.shop.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * Servlet implementation class ImageValidServlet
 */
@SuppressWarnings("restriction")
@WebServlet("/ImageValidServlet")
public class ImageValidAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageValidAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("image/jpeg"); //�����������ΪjpegͼƬ
		int width=70;
		int height=25;                     //��֤ͼƬ�Ŀ�ȣ��߶�
		Color back=getBack();
		Color front=getFront(back);
		String code=getStringCode();
		req.getSession().setAttribute("randadmin", code);  //���浽session����
        BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        Graphics2D g=bi.createGraphics();   //�õ�����
        g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));  //��������
        g.setColor(back);
        g.fillRect(0, 0, width, height);    //������
        g.setColor(front);
        g.drawString(code,12,20);           //���ַ�
        for(int i=0,n=random.nextInt(20);i<n;i++){
        	g.fillRect(random.nextInt(width),random.nextInt(height),1,1);
        	
        }                                   //��������20�����
        ServletOutputStream so=resp.getOutputStream();  //�õ������������ 
        JPEGImageEncoder je=JPEGCodec.createJPEGEncoder(so); //��ͼƬ���б����jpeg��ʽ
        je.encode(bi);                    
        so.flush();                         //ˢ�»���

		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected static Random random=new Random();

	/**
	 * @desc �õ�ͼƬ����ɫ
	 * @author aj
	 * @date 2011-3-30
	 * */
	protected static Color getBack(){
		//0.77.153
		//random.nextInt(255),random.nextInt(255),random.nextInt(255)
		return new Color(0,51,102);
	}
	/**
	 * @desc �����ɫ�ķ�ɫ
	 * @author aj
	 * @date 2011-3-30
	 * */
	protected static Color getFront(Color c){
		//0.77.153
		//255-c.getRed(),255-c.getGreen(),255-c.getBlue()
		return new Color(255,230,204);
		
	}
	/**
	 * @desc ��������ַ�
	 * @author aj
	 * @date 2011-3-30
	 * */
	protected static String getStringCode(){
		String old="23456789abcdefghijkmnpqrstuvwxyz"; //��֤ͼƬ���������ַ�
		StringBuffer sb=new StringBuffer();
		int j=0;
		for(int i=0;i<4;i++){
			j=random.nextInt(old.length());
			sb.append(old.substring(j,j+1));
		}
		
	   return sb.toString();	
	}

}
